<?php
!function_exists('adminmsg') && exit('Forbidden');
$housefieldsService = House::loadService('HouseFieldsService');
$houseViewUtility = House::loadClass('houseview', 'utility');
$areaField = $housefieldsService->getCompsiteFieldsByType('area');//所属区域
$plateField = $housefieldsService->getCompsiteFieldsByType('plate');//所在商圈
$avepriceField = $housefieldsService->getCompsiteFieldsByType('aveprice');//价格范围
$propertyrightField = $housefieldsService->getCompsiteFieldsByType('propertyright');//房屋产权
$propertyField = $housefieldsService->getCompsiteFieldsByType('property');//房屋类型	
$fitmentField = $housefieldsService->getCompsiteFieldsByType('paystatus');//交互标准
$amenitiesField = $housefieldsService->getCompsiteFieldsByType('amenities');//配套设实
$buildtypeField = $housefieldsService->getCompsiteFieldsByType('buildtype');//建筑类别
$housetypeField = $housefieldsService->getCompsiteFieldsByType('housetype');//建筑类别	
$acreageField = $housefieldsService->getCompsiteFieldsByType('acreage');
$budgetField = $housefieldsService->getCompsiteFieldsByType('budget');
$buildStatus = $houseViewUtility->buildStatus();//楼盘状态
$houseBaseUrl = "index.php?m=$m&";
if (empty($action)) {
	
	if (empty($_POST['step'])) {
		
		include_once (D_P . 'data/bbscache/house_config.php');
		InitGP(array(
			'page', 
			'area', 
			'plate', 
			'paystatus', 
			'status', 
			'ifopen', 
			'developer',
			'name'
		));
		$page < 1 && $page = 1;
		$perpage = 20;
		$plates = $housefieldsService->getPlateByAreaId($area);
		$data = array(
			'area' => $area, 
			'plate' => $plate, 
			//'aveprice'	=> $aveprice,
			//'property'	=> $property,
			'paystatus' => $paystatus, 
			'status' => $status, 
			'developer' => $developer, 
			'ifopen' => $ifopen, 
			'name' => $name
		);
		$houseinfo = House::LoadClass('HouseInfo');
		$where = $url_add = $houseinfo->paramCheck($data);
		
		if ($ifopen) {
			$where['ifopen'] = ($ifopen == 1) ? 1 : 0;
		}
		$pageUrl = $houseinfo->urlCompound($basename, $url_add);
		list($totalCount, $pages) = $houseinfo->getPages($where, $page, $perpage, $pageUrl . '&', true);
		$infodb = $houseinfo->getList($where, ($page - 1) * $perpage, $perpage);
		$developerdb = $houseinfo->getDeveloper();
		require_once PrintMode('info');
	
	} else {
		
		InitGP(array(
			'selid'
		), '', 2);
		!$selid && adminmsg('operate_error');
		$houseinfo = House::LoadClass('HouseInfo');
		
		if ($_POST['job'] == 'del') {
			
			$houseinfo->deleteByIds($selid);
		
		} elseif ($_POST['job'] == 'open' || $_POST['job'] == 'close') {
			
			$houseinfo->updateByIds(array(
				'ifopen' => ($_POST['job'] == 'open') ? 1 : 0
			), $selid);
		
		} else {
			$houseinfo->updateByIds(array(
				'recommend' => ($_POST['job'] == 'recommend') ? 1 : 0
			), $selid);
			$recommend = ($_POST['job'] == 'recommend') ? 1 : 0;
			if ($recommend) {
				$pwSQL = array();
				foreach ($selid as $key => $value) {
					$value = (int) $value;
					if (!$value) continue;
					$pwSQL[] = array(
						$value, 
						$timestamp
					);
				}
				$db->update('REPLACE INTO pw_house_recommend(hid,timestamp) VALUES ' . pwSqlMulti($pwSQL));
			} else {
				$db->update('DELETE FROM pw_house_recommend WHERE hid IN (' . pwImplode($selid) . ')');
			}
			$pageCache = getPageCache('list', '');
			$pageCache->deleteCache('recommendHouse');
		}
		adminmsg('operate_success');
	}

} elseif ($action == 'add') {
	
	if (empty($_POST['step'])) {
		
		$rt = array();
		$developerdb = array();
		$query = $db->query("SELECT id,name FROM pw_house_developer");
		while ($rt = $db->fetch_array($query)) {
			$developerdb[$rt['id']] = $rt['name'];
		}
		require_once PrintMode('info');
	
	} else {
		
		InitGP(array(
			'name', 
			'address', 
			'saletele', 
			'saleaddress', 
			'sellinglicense', 
			'opendate', 
			'deliverdate', 
			'plotratio', 
			'greenratio', 
			'carportfee', 
			'cleaningfee', 
			'propertycompany', 
			'traffic', 
			'seotitle', 
			'seokeywords', 
			'seodescription'
		));
		InitGP(array(
			'peripheral', 
			'introduction'
		), 'P', 0);
		InitGP(array(
			'isdimopendate',
			'isdimdeliverdate',
			'property', 
			'buildtype',
			'paystatus', 
			'area', 
			'plate', 
			'housetype', 
			'developer', 
			'aveprice', 
			'buildingarea', 
			'floorspace', 
			'carport', 
			'status'
		), 'P', 2);
		
		empty($name) && adminmsg('楼盘名称不能为空!');
		strlen($name) > 40 && adminmsg('楼盘名称长度不能超过40个字节!');
		if ($db->get_one("SELECT * FROM pw_house_info WHERE name=" . pwEscape($name))) {
			adminmsg('该楼盘名称已经存在!');
		}
		
		include_once (D_P . 'data/bbscache/house_config.php');
		$property = getBitValue($property, $propertyField);
		$paystatus = getBitValue($paystatus, $fitmentField);
		
		$opendate && !is_numeric($opendate) && $opendate = PwStrtoTime($opendate);
		$deliverdate && !is_numeric($deliverdate) && $deliverdate = PwStrtoTime($deliverdate);
		
		$buildtype = getBitValue($buildtype, $buildtypeField);
		$plotratio = round($plotratio, 2);
		$greenratio = round($greenratio, 2);
		$carportfee = round($carportfee, 2);
		$cleaningfee = round($cleaningfee, 2);
		$db->update("INSERT INTO pw_house_info SET " . pwSqlSingle(array(
			'name' => $name, 
			'ifopen' => 1, 
			'property' => $property, 
			'area' => $area, 
			'plate' => $plate, 
			'address' => $address, 
			'saletele' => $saletele, 
			'saleaddress' => $saleaddress, 
			'developer' => $developer, 
			'aveprice' => $aveprice, 
			'housetype' => $housetype, 
			'status' => $status, 
			'buildtype' => $buildtype, 
			'sellinglicense' => $sellinglicense, 
			'paystatus' => $paystatus, 
			'opendate' => $opendate,
			'isdimopendate'=>$isdimopendate,
			'deliverdate'=>$deliverdate,
			'isdimdeliverdate'=>$isdimdeliverdate,
			'buildingarea' => $buildingarea, 
			'floorspace' => $floorspace, 
			'plotratio' => $plotratio, 
			'greenratio' => $greenratio, 
			'carport' => $carport, 
			'carportfee' => $carportfee, 
			'cleaningfee' => $cleaningfee, 
			'adddate' => $timestamp
		)));
		
		$hid = $db->insert_id();
		
		require_once (M_P . 'lib/houselogo.class.php');
		if (PwUpload::getUploadNum()) {
			$logoimg = new HouseLogoUpload($hid);
			PwUpload::upload($logoimg);
			if ($logo = $logoimg->getLogo()) {
				$db->update("UPDATE pw_house_info SET logo=" . pwEscape($logo) . ' WHERE hid=' . pwEscape($hid));
			}
			$banner = new HouseBannerUpload($hid);
			PwUpload::upload($banner);
			if ($bannerimg = $banner->getBanner()) {
				$db->update("UPDATE pw_house_info SET banner=" . pwEscape($bannerimg) . ' WHERE hid=' . pwEscape($hid));
			}
		}
		
		$db->update("INSERT INTO pw_house_infoextra SET " . pwSqlSingle(array(
			'hid' => $hid, 
			'traffic' => $traffic, 
			'propertycompany' => $propertycompany, 
			'peripheral' => $peripheral, 
			'introduction' => $introduction, 
			'seotitle' => $seotitle, 
			'seokeywords' => $seokeywords, 
			'seodescription' => $seodescription
		)));
		$basename .= "&action=edit&hid=$hid";
		
		adminmsg('operate_success');
	}
} elseif ($action == 'edit') {
	
	InitGP(array(
		'hid'
	), '', 2);
	
	$rt = $db->get_one("SELECT *,ie.hid AS ifexists FROM pw_house_info i LEFT JOIN pw_house_infoextra ie USING(hid) WHERE i.hid=" . pwEscape($hid));
	empty($rt) && adminmsg('undefined_action');
	
	if (empty($_POST['step'])) {
		$plateField = $housefieldsService->getPlateByAreaId($rt['area']);
		$developerdb = array();
		$query = $db->query("SELECT id,name FROM pw_house_developer");
		while ($rs = $db->fetch_array($query)) {
			$developerdb[$rs['id']] = $rs['name'];
		}
		
		if ($rt['logo']) {
			$logo = geturl($rt['logo']);
		}
		if ($rt['banner']) {
			$banner = geturl($rt['banner']);
		}
		$rt['opendate'] = $rt['opendate'] ? get_date($rt['opendate'], 'Y-m-d') : '';
		$rt['deliverdate'] = $rt['deliverdate'] ? get_date($rt['deliverdate'], 'Y-m-d') : '';
		!ceil($rt['carportfee']) && $rt['carportfee'] = '暂无';
		!ceil($rt['cleaningfee']) && $rt['cleaningfee'] = '暂无';
		!$rt['aveprice'] && $rt['aveprice'] = '暂无';
		include_once (D_P . 'data/bbscache/house_config.php');
		require_once PrintMode('info');
	
	} else {
		
		InitGP(array(
			'name', 
			'address', 
			'saletele', 
			'saleaddress', 
			'sellinglicense', 
			'opendate', 
			'deliverdate', 
			'plotratio', 
			'greenratio', 
			'carportfee', 
			'cleaningfee', 
			'propertycompany', 
			'traffic', 
			'seotitle', 
			'seokeywords', 
			'seodescription'
		));
		InitGP(array(
			'peripheral', 
			'introduction'
		), 'P', 0);
		InitGP(array(
			'isdimopendate',
			'isdimdeliverdate',
			'property', 
			'buildtype', 
			'paystatus', 
			'area', 
			'plate', 
			'housetype', 
			'developer', 
			'aveprice', 
			'buildingarea', 
			'floorspace', 
			'carport', 
			'status'
		), 'P', 2);
		
		$basename .= "&action=edit&hid=$hid";
		
		empty($name) && adminmsg('楼盘名称不能为空!');
		strlen($name) > 40 && adminmsg('楼盘名称长度不能超过40个字节!');
		if ($name != $rt['name'] && $db->get_one("SELECT * FROM pw_house_info WHERE name=" . pwEscape($name))) {
			adminmsg('该楼盘名称已经存在!');
		}
		
		include_once (D_P . 'data/bbscache/house_config.php');
		$property = getBitValue($property, $propertyField);
		$buildtype = getBitValue($buildtype, $buildtypeField);
		$paystatus = getBitValue($paystatus, $fitmentField);
		
		$opendate && !is_numeric($opendate) && $opendate = PwStrtoTime($opendate);
		$deliverdate && !is_numeric($deliverdate) && $deliverdate = PwStrtoTime($deliverdate);
		
		$plotratio = round($plotratio, 2);
		$greenratio = round($greenratio, 2);
		$carportfee = round($carportfee, 2);
		$cleaningfee = round($cleaningfee, 2);
		
		$updatedb = array(
			'name' => $name, 
			'property' => $property, 
			'buildtype' => $buildtype, 
			'area' => $area, 
			'plate' => $plate, 
			'address' => $address, 
			'saletele' => $saletele, 
			'saleaddress' => $saleaddress, 
			'developer' => $developer, 
			'aveprice' => $aveprice, 
			'housetype' => $housetype, 
			'status' => $status, 
			'sellinglicense' => $sellinglicense, 
			'paystatus' => $paystatus, 
			'opendate' => $opendate, 
			'isdimopendate'=>$isdimopendate,
			'deliverdate'=>$deliverdate,
			'isdimdeliverdate'=>$isdimdeliverdate,
			'buildingarea' => $buildingarea, 
			'floorspace' => $floorspace, 
			'plotratio' => $plotratio, 
			'greenratio' => $greenratio, 
			'carport' => $carport, 
			'carportfee' => $carportfee, 
			'cleaningfee' => $cleaningfee
		);
		
		require_once (M_P . 'lib/houselogo.class.php');
		if (PwUpload::getUploadNum()) {
			$logo = new HouseLogoUpload($hid);
			PwUpload::upload($logo);
			if ($logoimg = $logo->getLogo()) {
				$updatedb['logo'] = $logoimg;
			}
			$banner = new HouseBannerUpload($hid);
			PwUpload::upload($banner);
			if ($bannerimg = $banner->getBanner()) {
				$updatedb['banner'] = $bannerimg;
			}
		}
		$db->update("UPDATE pw_house_info SET " . pwSqlSingle($updatedb) . ' WHERE hid=' . pwEscape($hid));
		
		$updatedb = array(
			'hid' => $hid, 
			'traffic' => $traffic, 
			'propertycompany' => $propertycompany, 
			'peripheral' => $peripheral, 
			'introduction' => $introduction, 
			'seotitle' => $seotitle, 
			'seokeywords' => $seokeywords, 
			'seodescription' => $seodescription
		);
		if ($rt['ifexists']) {
			$db->update("UPDATE pw_house_infoextra SET " . pwSqlSingle($updatedb) . ' WHERE hid=' . pwEscape($hid));
		} else {
			$db->update("REPLACT INTO pw_house_infoextra SET " . pwSqlSingle($updatedb));
		}
		
		adminmsg('operate_success');
	
	}
} elseif ($action == 'dellogo') {
	
	define('AJAX', 1);
	InitGP(array(
		'hid'
	));
	
	$rt = $db->get_one("SELECT logo FROM pw_house_info WHERE hid=" . pwEscape($hid));
	empty($rt) && adminmsg('undefined_action');
	
	pwDelatt($rt['logo']);
	$db->update("UPDATE pw_house_info SET logo='' WHERE hid=" . pwEscape($hid));
	
	echo 'ok';
	ajax_footer();

} elseif ($action == 'delbanner') {
	
	define('AJAX', 1);
	InitGP(array(
		'hid'
	));
	
	$rt = $db->get_one("SELECT banner FROM pw_house_info WHERE hid=" . pwEscape($hid));
	empty($rt) && adminmsg('undefined_action');
	
	pwDelatt($rt['banner']);
	$db->update("UPDATE pw_house_info SET banner='' WHERE hid=" . pwEscape($hid));
	
	echo 'ok';
	ajax_footer();

} elseif ($action == 'developer') {
	
	if (empty($_POST['step'])) {
		
		InitGP(array(
			'page'
		), '', 2);
		InitGP(array(
			'name'
		));
		$page < 1 && $page = 1;
		$perpage = 20;
		$_sql_where = '';
		$name && $_sql_where .= ' AND name LIKE ' . pwEscape('%' . $name . '%');
		
		$limit = pwLimit(($page - 1) * $perpage, $perpage);
		$total = $db->get_value("SELECT COUNT(*) AS sum FROM pw_house_developer WHERE 1{$_sql_where}");
		$pages = numofpage($total, $page, ceil($total / $perpage), "$basename&action=developer&");
		
		$devdb = array();
		$query = $db->query("SELECT * FROM pw_house_developer WHERE 1{$_sql_where} ORDER BY id $limit");
		while ($rt = $db->fetch_array($query)) {
			$devdb[] = $rt;
		}
		
		require_once PrintMode('info');
	
	} else {
		
		$basename .= '&action=developer';
		InitGP(array(
			'selid'
		), '', 2);
		empty($selid) && adminmsg('operate_error');
		
		if (is_array($selid)) {
			$houseinfo = House::LoadClass('HouseInfo');
			if ($info = $houseinfo->checkFieldData('developer', $selid, 'single')) {
				$devinfo = $db->get_one("SELECT * FROM pw_house_developer WHERE id = " . pwEscape($info['developer']));
				adminmsg($devinfo['name'] . '下存在楼盘，不能删除!');
			}
			$db->update("DELETE FROM pw_house_developer WHERE id IN (" . pwImplode($selid) . ')');
		}
		adminmsg('operate_success');
	}
} elseif ($action == 'adddeveloper') {
	
	$basename .= '&action=developer';
	InitGP(array(
		'name', 
		'level', 
		'address', 
		'zipcode', 
		'email', 
		'url', 
		'principal', 
		'linkman', 
		'tel', 
		'fax', 
		'introduction'
	));
	empty($name) && adminmsg('开发商名称不能为空!');
	
	if ($db->get_one("SELECT * FROM pw_house_developer WHERE name=" . pwEscape($name))) {
		adminmsg('该开发商已经存在!');
	}
	
	$db->update("INSERT INTO pw_house_developer SET " . pwSqlSingle(array(
		'name' => $name, 
		'level' => $level, 
		'address' => $address, 
		'zipcode' => $zipcode, 
		'email' => $email, 
		'url' => $url, 
		'principal' => $principal, 
		'linkman' => $linkman, 
		'tel' => $tel, 
		'fax' => $fax, 
		'introduction' => $introduction
	)));
	
	adminmsg('operate_success', $basename);

} elseif ($action == 'editdeveloper') {
	
	define('AJAX', 1);
	InitGP(array(
		'id'
	), '', 2);
	
	$rt = $db->get_one("SELECT * FROM pw_house_developer WHERE id=" . pwEscape($id));
	empty($rt) && adminmsg('undefined_action');
	
	if (empty($_POST['step'])) {
		
		$posthash = EncodeUrl("$basename&action=editdeveloper", true);
		require_once PrintMode('ajax_info');
		ajax_footer();
	
	} else {
		
		InitGP(array(
			'name', 
			'level', 
			'address', 
			'zipcode', 
			'email', 
			'url', 
			'principal', 
			'linkman', 
			'tel', 
			'fax', 
			'introduction'
		));
		empty($name) && adminmsg('开发商名称不能为空!');
		
		if ($name != $rt['name'] && $db->get_one("SELECT * FROM pw_house_developer WHERE name=" . pwEscape($name))) {
			adminmsg('该开发商已经存在!');
		}
		$db->update("UPDATE pw_house_developer SET " . pwSqlSingle(array(
			'name' => $name, 
			'level' => $level, 
			'address' => $address, 
			'zipcode' => $zipcode, 
			'email' => $email, 
			'url' => $url, 
			'principal' => $principal, 
			'linkman' => $linkman, 
			'tel' => $tel, 
			'fax' => $fax, 
			'introduction' => $introduction
		)) . ' WHERE id=' . pwEscape($id));
		
		Showmsg("操作成功!\tjump\tadmin.php?adminjob=mode&admintype=house_info&action=developer");
	}
} elseif ($action == 'deldeveloper') {
	
	define('AJAX', 1);
	InitGP(array(
		'id'
	), '', 2);
	$posthash = EncodeUrl("$basename&action=developer", true);
	require_once PrintMode('ajax_info');
	ajax_footer();

} elseif ($action == 'feed') {
	
	InitGP(array(
		'hid'
	), null, 2);
	
	$rt = $db->get_one("SELECT * FROM pw_house_info WHERE hid=" . pwEscape($hid));
	empty($rt) && adminmsg('undefined_action');
	
	if (empty($_POST['step'])) {
		
		InitGP(array(
			'page'
		), null, 2);
		$page < 1 && $page = 1;
		$perpage = 20;
		$total = $db->get_value("SELECT COUNT(*) AS sum FROM pw_house_feed WHERE hid=" . pwEscape($hid));
		$pages = numofpage($total, $page, ceil($total / $perpage), "$basename&action=feed&hid=$hid&");
		$limit = pwLimit(($page - 1) * $perpage, $perpage);
		
		$feeddb = array();
		$query = $db->query("SELECT * FROM pw_house_feed WHERE hid=" . pwEscape($hid) . " ORDER BY postdate DESC $limit");
		while ($rs = $db->fetch_array($query)) {
			$rs['postdate'] = get_date($rs['postdate']);
			$feeddb[] = $rs;
		}
		require_once PrintMode('info');
	
	} else {
		
		$basename .= "&action=feed&hid=$hid";
		InitGP(array(
			'selid'
		), null, 2);
		!$selid && adminmsg('operate_error');
		$pageCache = getPageCache('info', $hid);
		$pageCache->deleteCache('newFeed');
		if (is_array($selid)) {
			$db->update("DELETE FROM pw_house_feed WHERE id IN (" . pwImplode($selid) . ')');
		}
		adminmsg('operate_success');
	}

} elseif ($action == 'addfeed') {
	
	InitGP(array(
		'hid'
	), null, 2);
	InitGP(array(
		'postdate', 
		'descrip'
	));
	
	$basename .= '&action=feed&hid=' . $hid;
	
	$rt = $db->get_one("SELECT hid FROM pw_house_info WHERE hid=" . pwEscape($hid));
	empty($rt) && adminmsg('undefined_action');
	
	$postdate = $postdate ? pwStrtoTime($postdate) : $timestamp;
	$pageCache = getPageCache('info', $hid);
	$pageCache->deleteCache('newFeed');
	$db->update("INSERT INTO pw_house_feed SET " . pwSqlSingle(array(
		'hid' => $hid, 
		'postdate' => $postdate, 
		'descrip' => $descrip, 
		'adddate' => $timestamp
	)));
	
	adminmsg('operate_success');

} elseif ($action == 'editfeed') {
	
	define('AJAX', 1);
	InitGP(array(
		'id'
	), null, 2);
	$rt = $db->get_one("SELECT * FROM pw_house_feed WHERE id=" . pwEscape($id));
	empty($rt) && adminmsg('undefined_action');
	
	if (empty($_POST['step'])) {
		
		$posthash = EncodeUrl("$basename&action=editfeed", true);
		$rt['postdate'] = get_date($rt['postdate']);
		require_once PrintMode('ajax_info');
		ajax_footer();
	
	} else {
		
		InitGP(array(
			'postdate', 
			'descrip'
		));
		
		$postdate = $postdate ? pwStrtoTime($postdate) : $timestamp;
		
		$hid = $db->get_value('SELECT hid FROM pw_house_feed WHERE id=' . pwEscape($id));
		if (!$hid) Showmsg('undefined_action');
		$pageCache = getPageCache('info', $hid);
		$pageCache->deleteCache('newFeed');
		
		$db->update("UPDATE pw_house_feed SET " . pwSqlSingle(array(
			'postdate' => $postdate, 
			'descrip' => $descrip
		)) . ' WHERE id=' . pwEscape($id));
		
		Showmsg('ajaxma_success');
	}
} elseif ($action == 'colony') {
	
	InitGP(array(
		'hid'
	), null, 2);
	
	$rt = $db->get_one("SELECT hid,cyid FROM pw_house_info WHERE hid=" . pwEscape($hid));
	empty($rt) && adminmsg('undefined_action');
	
	if (empty($_POST['step'])) {
		
		$perpage = 15;
		$cnclassdb = $cydb = array();
		$query = $db->query("SELECT * FROM pw_cnclass WHERE ifopen='1'");
		while ($rs = $db->fetch_array($query)) {
			$cnclassdb[] = $rs;
		}
		
		$total = $db->get_value("SELECT COUNT(*) AS sum FROM pw_colonys");
		$limit = pwLimit(0, $perpage);
		$query = $db->query("SELECT id,cname FROM pw_colonys ORDER BY id DESC $limit");
		while ($rs = $db->fetch_array($query)) {
			$cydb[] = $rs;
		}
		
		$cname = '';
		if ($rt['cyid']) {
			$cname = $db->get_value("SELECT cname FROM pw_colonys WHERE id=" . pwEscape($rt['cyid']));
		}
		
		require_once PrintMode('info');
	}
} elseif ($action == 'setcolony') {
	
	if (empty($_POST['step'])) {
		
		InitGP(array(
			'page', 
			'cid'
		), null, 2);
		InitGP(array(
			'cname'
		));
		$page < 1 && $page = 1;
		$perpage = 15;
		
		$_sql_where = $url_add = '';
		if ($cid) {
			$_sql_where .= ' AND classid=' . pwEscape($cid);
			$url_add .= '&cid=' . $cid;
		}
		if ($cname) {
			$_sql_where .= ' AND cname LIKE' . pwEscape(str_replace('*', '%', $cname));
			$url_add .= '&cname=' . $cname;
		}
		
		$cydb = array();
		$total = $db->get_value("SELECT COUNT(*) AS sum FROM pw_colonys WHERE 1{$_sql_where}");
		$limit = pwLimit(($page - 1) * $perpage, $perpage);
		$query = $db->query("SELECT id,cname FROM pw_colonys WHERE 1{$_sql_where} ORDER BY id DESC $limit");
		while ($rs = $db->fetch_array($query)) {
			$cydb[] = $rs;
		}
		$prepage = $page - 1;
		$nextpage = $page + 1;
		require_once PrintMode('ajax_info');
		ajax_footer();
	
	} elseif ($_POST['step'] == '2') {
		
		InitGP(array(
			'cyid', 
			'hid'
		), null, 2);
		$basename .= "&action=colony&hid=$hid";
		
		$rt = $db->get_one("SELECT id FROM pw_colonys WHERE id=" . pwEscape($cyid));
		empty($rt) && adminmsg('请选择关联小区!');
		
		$db->update("UPDATE pw_house_info SET cyid=" . pwEscape($cyid) . ' WHERE hid=' . pwEscape($hid));
		$pageCache = getPageCache('info', $hid);
		$pageCache->deleteCache('newThread');
		adminmsg('operate_success');
	
	} else {
		
		InitGP(array(
			'hid'
		), null, 2);
		$basename .= "&action=colony&hid=$hid";
		
		$db->update("UPDATE pw_house_info SET cyid='' WHERE hid=" . pwEscape($hid));
		$pageCache = getPageCache('info', $hid);
		$pageCache->deleteCache('newThread');
		
		adminmsg('operate_success');
	}
} elseif ($action == 'nav') {
	
	InitGP(array(
		'hid'
	), '', 2);
	$rt = $db->get_one("SELECT a.hid,b.hid AS ifexists,b.nav FROM pw_house_info a LEFT JOIN pw_house_infoextra b USING(hid) WHERE a.hid=" . pwEscape($hid));
	empty($rt) && adminmsg('undefined_action');
	
	$navdb = $rt['nav'] ? unserialize($rt['nav']) : array();
	
	if (empty($_POST['step'])) {
		
		require_once PrintMode('info');
	
	} else {
		
		InitGP(array(
			'selid', 
			'view'
		), 'P', 2);
		$basename .= '&action=nav&hid=' . $hid;
		
		if ($view) {
			foreach ($view as $key => $value) {
				$navdb[$key]['view'] = $value;
			}
		}
		if ($selid) {
			foreach ($selid as $key => $value) {
				unset($navdb[$value]);
			}
		}
		if ($navdb) {
			navSort($navdb);
			$navdb = serialize($navdb);
		} else {
			$navdb = '';
		}
		$db->update("UPDATE pw_house_infoextra SET nav=" . pwEscape($navdb) . ' WHERE hid=' . pwEscape($hid));
		
		adminmsg('operate_success');
	}

} elseif ($action == 'addnav') {
	
	define('AJAX', 1);
	InitGP(array(
		'hid'
	), null, 2);
	$rt = $db->get_one("SELECT a.hid,b.hid AS ifexists,b.nav FROM pw_house_info a LEFT JOIN pw_house_infoextra b USING(hid) WHERE a.hid=" . pwEscape($hid));
	empty($rt) && adminmsg('undefined_action');
	
	InitGP(array(
		'title', 
		'link', 
		'alt', 
		'color'
	), 'P');
	InitGP(array(
		'view', 
		'b', 
		'i', 
		'u', 
		'url_target'
	), 'P', 2);
	empty($title) && adminmsg("nav_empty_title");
	
	$navdb = $rt['nav'] ? unserialize($rt['nav']) : array();
	$style = $color . "|" . $b . "|" . $i . "|" . $u;
	
	$navdb[] = array(
		'title' => $title, 
		'link' => $link, 
		'alt' => $alt, 
		'style' => $style, 
		'target' => $url_target, 
		'view' => $view
	);
	navSort($navdb);
	
	if ($rt['ifexists']) {
		$db->update("UPDATE pw_house_infoextra SET nav=" . pwEscape(serialize($navdb)) . ' WHERE hid=' . pwEscape($hid));
	} else {
		$db->update("INSERT INTO pw_house_infoextra SET " . pwSqlSingle(array(
			'hid' => $hid, 
			'nav' => serialize($navdb)
		)));
	}
	Showmsg('ajaxma_success');

} elseif ($action == 'editnav') {
	
	define('AJAX', 1);
	InitGP(array(
		'hid', 
		'id'
	), null, 2);
	$rt = $db->get_one("SELECT a.hid,b.hid AS ifexists,b.nav FROM pw_house_info a LEFT JOIN pw_house_infoextra b USING(hid) WHERE a.hid=" . pwEscape($hid));
	empty($rt) && adminmsg('undefined_action');
	
	$navdb = $rt['nav'] ? unserialize($rt['nav']) : array();
	
	if (!isset($navdb[$id])) {
		adminmsg('undefined_action');
	}
	
	if (empty($_POST['step'])) {
		
		$r = $navdb[$id];
		$style_array = explode("|", $r['style']);
		$style_color = $style_array[0];
		$fontstyle = "color:$style_color;";
		if ($style_array[1]) {
			$b_check = "checked";
			$fontstyle .= "font-weight='bolder';";
		}
		if ($style_array[2]) {
			$i_check = "checked";
			$fontstyle .= "font-style='italic';";
		}
		if ($style_array[3]) {
			$u_check = "checked";
			$fontstyle .= "text-decoration='underline';";
		}
		$r['target'] == 1 ? $blank_check = "checked" : $self_check = "checked";
		$posthash = EncodeUrl("$basename&action=editnav", true);
		
		require_once PrintMode('ajax_info');
		ajax_footer();
	
	} else {
		
		InitGP(array(
			'title', 
			'link', 
			'alt', 
			'color'
		), 'P');
		InitGP(array(
			'view', 
			'b', 
			'i', 
			'u', 
			'url_target'
		), 'P', 2);
		empty($title) && adminmsg("nav_empty_title");
		
		$style = $color . "|" . $b . "|" . $i . "|" . $u;
		
		$navdb[$id] = array(
			'title' => $title, 
			'link' => $link, 
			'alt' => $alt, 
			'style' => $style, 
			'target' => $url_target, 
			'view' => $view
		);
		navSort($navdb);
		
		$db->update("UPDATE pw_house_infoextra SET nav=" . pwEscape(serialize($navdb)) . ' WHERE hid=' . pwEscape($hid));
		Showmsg('ajaxma_success');
	
	}

} elseif ($action == 'imagetype') {
	require_once M_P . 'admin/imagetype.php';
} elseif ($action == 'album') {
	require_once M_P . 'admin/album.php';
} elseif ($action == 'video') {
	require_once M_P . 'admin/video.php';
} elseif ($action == 'information') {
	require_once M_P . 'admin/information.php';
} elseif ($action == 'map') {
	InitGP(array(
		'hid', 
		'step'
	), GP);
	if (!empty($step)) {
		require_once M_P . 'admin/housemap.php';
	} else {
		include PrintMode('info');
	}
} elseif ($action == 'housemap') {
	require_once M_P . 'admin/housemap.php';
} elseif ($action == 'pricedetail') {
	require_once M_P . 'admin/pricedetail.php';
} elseif ($action == 'evaluation') {
	require_once M_P . 'admin/evaluation.php';
}else if($action == 'getplate'){
	$HouseFieldsService = House::LoadService('HouseFieldsService');
	InitGP(array('area'),'GP',2);
	$plates = $HouseFieldsService->getPlateByAreaId($area);//所有板块
	echo pwJsonEncode($plates);
	ajax_footer();
}

function getBitValue($select, $source) {
	$bit = 0;
	foreach ($select as $key => $value) {
		if (isset($source[$value])) {
			$bit |= (1 << ($value - 1));
		}
	}
	return $bit;
}

function navSort(&$navdb) {
	usort($navdb, "navcmd");
	foreach ($navdb as $key => $value) {
		$navdb[$key]['view'] = $key + 1;
	}
}

function navcmd($a, $b) {
	if ($a['view'] == $b['view']) return 0;
	return ($a['view'] < $b['view']) ? -1 : 1;
}
?>